
package com.cake.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cake.base.util.ReturnUtils;
import com.cake.entity.XxAdmin;
import com.cake.service.inter.IXxAdminService;
import com.cake.service.inter.IXxRoleService;

/**
 * 管理员控制层
 * @author master
 *
 */
@RestController
@RequestMapping(value = "admin")
public class AdminController {
	
	@Autowired
	private IXxAdminService xxAdminService;
	
	@Autowired 
	private IXxRoleService xxRoleService;
	
	/**
	 * 查询管理人员列表信息
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "selectSystemAdmins")
	public Object selectSystemAdmins(@RequestBody XxAdmin xxAdmin) throws Exception{
		return xxAdminService.selectSystemAdmins(xxAdmin);
	}
	
	/**
	 * 根据id查询管理员信息
	 * @param jsonStr
	 * @return
	 */
	@PostMapping(value = "selectSystemAdminById")
	public Object selectSystemAdminById(Long adminId){
		return xxAdminService.selectSystemAdminById(adminId);
	}
	
	/**
	 * 修改后台操作人员信息
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "insertOrUpdateAdmin")
	public Object updateAmind(@RequestBody XxAdmin xxAdmin) throws Exception{
		String rolesStrings = xxAdmin.getRolesStr();
		if(xxAdmin.getId() == null){
			xxAdminService.insert(xxAdmin);
		}else{
			xxAdminService.updateByPrimaryKeySelective(xxAdmin);
		}
		/**
		 * 角色字符串串持久化处理
		 */
		if(rolesStrings.length() > 0){
			String[] roles = rolesStrings.split(",");
			xxRoleService.updateAdminRoles(roles, xxAdmin.getId());
		}
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
	/**
	 * 查询当前登陆用户拥有的菜单权限
	 * @param request1
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectAdminMenus")
	public Object selectAdminMenus(HttpServletRequest request) throws Exception{
//		XxAdmin xxAdmin =  (XxAdmin) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
//		return ReturnUtils.forNoramlSuccess(xxAdminService.selectAdminMenus(xxAdmin.getId()));
//		xxAdminService.selectByPrimaryKey(xxAdmin.getId());
		Map<String, Object> map = new HashMap<>();
		/**
		 * 暂写死一个后天登陆用户
		 */
		map.put("menus", xxAdminService.selectAdminMenus(1L));
		XxAdmin xxAdmin = xxAdminService.selectByPrimaryKey(1L);
		map.put("isLock", xxAdmin.getIsLocked() ? 1 : 0);
		return ReturnUtils.forNoramlSuccess(map);
	}
	
	/**
	 * 修改密码
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "updatePassword")
	public Object updatePassword(@RequestBody XxAdmin xxAdmin) throws Exception{
		XxAdmin selectAdmin = xxAdminService.selectByPrimaryKey(xxAdmin.getId());
		if(null == selectAdmin){
			return ReturnUtils.forNoramlError("500", "111111", "无此用户");
		}
//		if(StringUtils.isEmpty(selectAdmin.getPassword())){
//			selectAdmin.setPassword(xxAdmin.getPassword());
//		}else{
//			if(!selectAdmin.getPassword().equals(xxAdmin.getOldPassword())){
//				return ReturnUtils.forNoramlError("500", "111111", "原密码错误");
//			}else{
//				
//			}
		selectAdmin.setPassword(xxAdmin.getPassword());
		xxAdminService.updateByPrimaryKeySelective(selectAdmin);
		return ReturnUtils.forNoramlSuccess(null);
//		}
	}
}
